

local DIY = require 'packages.diy_utility.diy_utility'

local guwo = fk.CreateSkill {
  name = "inker_guwo",
  tags = { Skill.Compulsory },
  dynamic_desc = function(self, player)
    if player and DIY.getSwitchState(player, "inker_guwo") ~= 0 then
      local state = DIY.getSwitchState(player, "inker_guwo")
      return "inker_guwo_dynamic" .. state
    else
      return "inker_guwo"
    end
  end
}

guwo:addAcquireEffect(function (self, player, is_start)
  DIY.setSwitchState(player, guwo.name, 1, 3)
  if is_start then return end
  player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
    local data = e.data
    if data.from == player and data.card then
      player.room:setPlayerMark(player, "@inker_guwo_number", data.card.number)
      return true
    end
  end, 0)
end)
guwo:addLoseEffect(function (self, player, is_death)
  DIY.removeSwitchSkill(player, guwo.name)
  player.room:setPlayerMark(player, "@inker_guwo_number", 0)
  player.room:setPlayerMark(player, "@inker_guwo_gu", 0)
end)

guwo:addEffect(fk.CardUseFinished, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(guwo.name) and data.extra_data and data.extra_data.inker_guwo_increased
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local state = DIY.getSwitchState(player, guwo.name)
    DIY.changeSwitchState(player, guwo.name)
    if state == 1 then
      room:addPlayerMark(player, "@inker_guwo_gu")
      return
    end

    local diff = player:getHandcardNum() - player.hp
    if diff > 0 then
      room:recover{
        who = player,
        num = diff,
        skillName = guwo.name
      }
    elseif diff < 0 then
      room:loseHp(player, -diff, guwo.name)
    end
    if state == 2 or player.dead then return end

    diff = player:getLostHp() - player:getHandcardNum()
    if diff > 0 then
      player:drawCards(diff, guwo.name)
    elseif diff < 0 then
      room:askToDiscard(player, {
        min_num = -diff,
        max_num = -diff,
        skill_name = guwo.name,
      })
    end
  end
})
guwo:addEffect(fk.CardUsing, {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    if target == player and player:hasSkill(guwo.name, true) then
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getMark("@inker_guwo_number")
    local num = data.card.number
    if mark > 0 and num > 0 and mark < num then
      data.extra_data = data.extra_data or {}
      data.extra_data.inker_guwo_increased = true
    end
    player.room:setPlayerMark(player, "@inker_guwo_number", data.card.number)
  end,
})

Fk:loadTranslationTable{
  ["inker_guwo"] = "孤我",
  [":inker_guwo"] = "转换技，锁定技，当你使用牌结算后，①②③若与你上一使用牌点数递增，②③则将体力调整至手牌数，③手牌调整至已失去体力值数。<br>" ..
  "<a href=':programmer_inker_guwo'>实际上……？</a>",

  [":inker_guwo_dynamic1"] = "转换技，锁定技，当你使用牌结算后，<font color=\"#E0DB2F\">①②③若与你上一使用牌点数递增，则获得一枚“孤”，</font>②③将体力调整至手牌数，③手牌调整至已失去体力值数。<br>" ..
  "<a href=':programmer_inker_guwo1'>实际上……？</a>",
  [":inker_guwo_dynamic2"] = "转换技，锁定技，当你使用牌结算后，<font color=\"#E0DB2F\">①②③若与你上一使用牌点数递增，②③则将体力调整至手牌数，</font>③手牌调整至已失去体力值数。<br>" ..
  "<a href=':programmer_inker_guwo2'>实际上……？</a>",
  [":inker_guwo_dynamic3"] = "转换技，锁定技，当你使用牌结算后，<font color=\"#E0DB2F\">①②③若与你上一使用牌点数递增，②③则将体力调整至手牌数，③手牌调整至已失去体力值数。</font><br>" ..
  "<a href=':programmer_inker_guwo3'>实际上……？</a>",

  [":programmer_inker_guwo"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递增，你依次执行前序号项：<br>" ..
  "①获得一枚“孤”；<br>" ..
  "②将体力调整至手牌数；<br>" ..
  "③将手牌调整至已失去体力值数。",
  [":programmer_inker_guwo1"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递增，你依次执行前一项：<br><font color=\"#E0DB2F\">" ..
  "①获得一枚“孤”；</font><br>" ..
  "②将体力调整至手牌数；<br>" ..
  "③将手牌调整至已失去体力值数。",
  [":programmer_inker_guwo2"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递增，你依次执行前两项：<br><font color=\"#E0DB2F\">" ..
  "<s>①获得一枚“孤”；</s><br>" ..
  "②将体力调整至手牌数；</font><br>" ..
  "③将手牌调整至已失去体力值数。",
  [":programmer_inker_guwo3"] = "转换技，锁定技，当你使用牌结算后，若与你上一使用牌点数递增，你依次执行前三项：<br><font color=\"#E0DB2F\">" ..
  "<s>①获得一枚“孤”；</s><br>" ..
  "②将体力调整至手牌数；<br>" ..
  "③将手牌调整至已失去体力值数。</font>",

  ["@inker_guwo_number"] = "孤我",
  ["@inker_guwo_gu"] = "孤",
}

return guwo
